package com.dream.testtubebaby_doctor;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dream.testtubebaby_doctor.adapter.ProductShoppingCartAdapter;
import com.dream.testtubebaby_doctor.data.product.ShopCartDao;
import com.dream.testtubebaby_doctor.data.product.ShoppingCartItemData;

public class MyShoppingCartActivity extends AppCompatActivity implements
		OnClickListener, ProductShoppingCartAdapter.onCheckedChanged {
	private ListView shopping_cart_listview;
	private ProductShoppingCartAdapter mProductShoppingCartAdapter;
	private ArrayList<ShoppingCartItemData> mShoppingCartItemDatas = new ArrayList<ShoppingCartItemData>();
	private CheckBox shop_cart_check_all;
	private TextView shop_cart_Allprice, shop_cart_buy_or_del, top_edit_tv;
	private double price_value;
	private boolean isEditFlag = false;
	private ShopCartDao cartDao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myshoppingcart_view);
		initView();
		initListview();
	}

	private void initListview() {
		shopping_cart_listview = (ListView) findViewById(R.id.shopping_cart_listview);
		cartDao = new ShopCartDao(this);
		List<ShoppingCartItemData> cartItemDatas = cartDao.list();
		mShoppingCartItemDatas.addAll(cartItemDatas);
		mProductShoppingCartAdapter = new ProductShoppingCartAdapter(this,
				mShoppingCartItemDatas, R.layout.product_shopping_cart_itemview);
		shopping_cart_listview.setAdapter(mProductShoppingCartAdapter);
		mProductShoppingCartAdapter.setOnCheckedChanged(this);
		top_edit_tv = (TextView) findViewById(R.id.top_edit_tv);
		top_edit_tv.setOnClickListener(this);
	}

	private void initView() {
		ImageView top_back_img = (ImageView) findViewById(R.id.top_back_img);
		top_back_img.setOnClickListener(this);
		TextView top_title_tv = (TextView) findViewById(R.id.top_title_tv);
		top_title_tv.setText(R.string.myshopingcart_title);
		shop_cart_check_all = (CheckBox) findViewById(R.id.shop_cart_check_all);
		shop_cart_Allprice = (TextView) findViewById(R.id.shop_cart_Allprice);
		shop_cart_buy_or_del = (TextView) findViewById(R.id.shop_cart_buy_or_del);
		shop_cart_check_all
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						for (int i = 0; i < mShoppingCartItemDatas.size(); i++) {
							mShoppingCartItemDatas.get(i).setCheck(isChecked);
						}
						mProductShoppingCartAdapter.notifyDataSetChanged();
						shopping_cart_listview.invalidate();
					}
				});
		shop_cart_buy_or_del.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_back_img:
			finish();
			break;
		case R.id.top_edit_tv:
			isEditFlag = !isEditFlag;
			updateEditView();
			break;
		case R.id.shop_cart_buy_or_del:
			if (isEditFlag) {
				for (int i = 0; i < mShoppingCartItemDatas.size(); i++) {
					if (mShoppingCartItemDatas.get(i).isCheck()) {
						cartDao.deleteOneProduct(mShoppingCartItemDatas.get(i)
								.getProduct_id());
					}
				}
				updateData();
				isEditFlag = false;
			} else {

			}
			updateEditView();
			break;
		default:
			break;
		}
	}

	private void updateData() {
		mShoppingCartItemDatas.clear();
		List<ShoppingCartItemData> cartItemDatas = cartDao.list();
		mShoppingCartItemDatas.addAll(cartItemDatas);
		mProductShoppingCartAdapter.notifyDataSetChanged();
	}

	private void updateEditView() {
		if (isEditFlag) {
			top_edit_tv.setText(R.string.myshopingcart_complete);
			shop_cart_buy_or_del.setText(R.string.myshopingcart_delete);
		} else {
			top_edit_tv.setText(R.string.myshopingcart_edit);
			shop_cart_buy_or_del.setText(R.string.product_settle);
		}
	}

	@Override
	public void getChoiceData(ShoppingCartItemData item) {
		if (item.isCheck()) {
			price_value += item.getProduct_price();
		} else {
			price_value -= item.getProduct_price();
		}
		shop_cart_Allprice.setText(getResources().getString(
				R.string.product_total, String.valueOf(price_value)));
	}
}
